package com.woniu.microworkorderserver.workgroupmag.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.microcommonsserver.entity.WorkGroupInfoEntity;
import com.woniu.microcommonsserver.entity.WorkgroupUserInfoEntity;
import com.woniu.microcommonsserver.param.PageRequest;
import com.woniu.microworkorderserver.workgroupmag.mapper.WorkGroupMapper;
import com.woniu.microworkorderserver.workgroupmag.mapper.WorkGroupUserMapper;
import com.woniu.microworkorderserver.workgroupmag.service.WorkGroupService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Service
public class WorkGroupServiceImpl implements WorkGroupService {

    @Resource
    private WorkGroupMapper workGroupMapper;
    @Resource
    private WorkGroupUserMapper workGroupUserMapper;

    @Override
    public long save(WorkGroupInfoEntity workGroupInfoEntity, List<Integer> list) {
        workGroupMapper.save(workGroupInfoEntity);
        list.forEach(e -> {
            WorkgroupUserInfoEntity entity = new WorkgroupUserInfoEntity();
            entity.setFkWorkGroupId(workGroupInfoEntity.getId());
            entity.setFkUserId(e.longValue());
            workGroupUserMapper.save(entity);
        });
        return 0;
    }

    @Override
    public PageInfo<WorkGroupInfoEntity> findPage(PageRequest page, WorkGroupInfoEntity entity) {
        return PageHelper.startPage(page.getPageNum(),page.getLimit())
                .doSelectPageInfo(() -> workGroupMapper.findPage(page, entity));
    }

    @Override
    public void delete(long workGroupId) {
        workGroupMapper.delete(workGroupId);
        workGroupUserMapper.delete(workGroupId);
    }

    @Override
    public void update(WorkGroupInfoEntity entity,List<Integer> list) {
        workGroupMapper.update(entity);
        workGroupUserMapper.delete(entity.getId());
        if(list == null){
            return;
        }
        list.forEach(e -> {
            WorkgroupUserInfoEntity userInfoEntity = new WorkgroupUserInfoEntity();
            userInfoEntity.setFkWorkGroupId(entity.getId());
            userInfoEntity.setFkUserId(Long.valueOf(e));
            workGroupUserMapper.save(userInfoEntity);
        });
    }
}
